package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.User;
import com.itheima.pojo.aaa;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {


    @Autowired
    private UserService userService;

	//分页查询用户
    @GetMapping("/{currentPage}/{pageSize}")
    public Result selectByPage(@PathVariable  int currentPage,@PathVariable int pageSize) throws IOException {
       //创建一个分页构造器

        System.out.println("111111");
        Page<User> userPage = new Page<>(currentPage,pageSize);
        userService.page(userPage);
        List records = userPage.getRecords();
        long total = userPage.getTotal();
        return new  Result(Code.GET_OK,new aaa(total,records));
    }
    /**
     * 保存用户
     * @param user 用户数据
     */
    @PostMapping
    public Result add(@RequestBody  User user) {
        userService.save(user);
        return new  Result(Code.SAVE_OK,user);
    }

    /**
     * 根据用户id查询用户
     * @param id 用户id
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {

        userService.removeById(id);
        return new  Result(Code.DELETE_OK,id);
    }

    /**
     * 更新用户
     * @param user 用户数据
     */
    @PutMapping
    public Result update(@RequestBody User user) {
        userService.updateById(user);

        return new  Result(Code.UPDATE_OK,user);
    }

}
